package com.czh.subject.application.controller;



import com.czh.subject.application.convert.SubjectLabelConverter;
import com.czh.subject.application.dto.SubjectLabelDTO;
import com.czh.subject.common.entity.ResponseResult;
import com.czh.subject.domain.entity.SubjectLabelBO;
import com.czh.subject.domain.service.SubjectLabelDomainService;
import com.google.common.base.Preconditions;
import jakarta.annotation.Resource;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 题目标签表(SubjectLabel)表控制层
 *
 * @author caozaihui
 * @since 2024-07-30 15:00:37
 */
@RestController
@RequestMapping("/subject/label")
@Log4j2
public class SubjectLabelController {

    @Resource
    private SubjectLabelDomainService subjectLabelDomainService;

    @PostMapping("/add")
    public ResponseResult<Boolean> add(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            Preconditions.checkNotNull(subjectLabelDTO.getLabelName(),"标签名称不能为空!");
            Preconditions.checkNotNull(subjectLabelDTO.getCategoryId(),"标签分类不能为空!");
            Preconditions.checkNotNull(subjectLabelDTO.getSortNum(),"标签排序不能为空!");
            SubjectLabelBO subjectLabelBO = SubjectLabelConverter.INSTANCE.convert(subjectLabelDTO);

            return ResponseResult.success(subjectLabelDomainService.addSubjectLabel(subjectLabelBO));
        } catch (Exception e) {
            log.error("SubjectLabelController.add.error:{}", e.getMessage(), e);
            return ResponseResult.error(e.getMessage());
        }
    }
    @PostMapping("/update")
    public ResponseResult<Boolean> update(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            Preconditions.checkNotNull(subjectLabelDTO.getLabelName(),"标签名称不能为空!");
            Preconditions.checkNotNull(subjectLabelDTO.getId(),"标签id不能为空!");
            Preconditions.checkNotNull(subjectLabelDTO.getSortNum(),"标签排序不能为空!");
            SubjectLabelBO subjectLabelBO = SubjectLabelConverter.INSTANCE.convert(subjectLabelDTO);

            return ResponseResult.success(subjectLabelDomainService.updateSubjectLabel(subjectLabelBO));
        } catch (Exception e) {
            log.error("SubjectLabelController.update.error:{}", e.getMessage(), e);
            return ResponseResult.error(e.getMessage());
        }
    }
    @PostMapping("/delete")
    public ResponseResult<Boolean> delete(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            Preconditions.checkNotNull(subjectLabelDTO.getId(),"标签id不能为空!");
            SubjectLabelBO subjectLabelBO = SubjectLabelConverter.INSTANCE.convert(subjectLabelDTO);

            return ResponseResult.success(subjectLabelDomainService.deleteSubjectLabel(subjectLabelBO));
        } catch (Exception e) {
            log.error("SubjectLabelController.delete.error:{}", e.getMessage(), e);
            return ResponseResult.error(e.getMessage());
        }
    }

    /**
     * 根据分类查询标签
     * @param subjectLabelDTO
     * @return List<SubjectLabelDTO> TODO tip 根据一级分类或者二级分类
     */

    @PostMapping("/queryLabelByCategoryId")
    public ResponseResult<List<SubjectLabelDTO>> queryLabelByCategoryId(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            Preconditions.checkNotNull(subjectLabelDTO.getCategoryId(),"分类id不能为空!");
            SubjectLabelBO subjectLabelBO = SubjectLabelConverter.INSTANCE.convert(subjectLabelDTO);
            List<SubjectLabelBO> subjectLabelBOList = subjectLabelDomainService.queryLabelByCategoryId(subjectLabelBO);
            return ResponseResult.success(SubjectLabelConverter.INSTANCE.convert(subjectLabelBOList));
        } catch (Exception e) {
            log.error("SubjectLabelController.queryLabelByCategoryId.error:{}", e.getMessage(), e);
            return ResponseResult.error(e.getMessage());
        }
    }
}

